package com.zzyl.nursing.controller.member;

import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.domain.Elder;
import com.zzyl.nursing.domain.FamilyMemberElder;
import com.zzyl.nursing.dto.FamilyMemberElderDto;
import com.zzyl.nursing.dto.UserLoginRequestDto;
import com.zzyl.nursing.service.IFamilyMemberService;
import com.zzyl.nursing.vo.FamilyMemberELderVo;
import com.zzyl.nursing.vo.LoginVo;
import com.zzyl.nursing.vo.health.HealthDataItemVo;
import com.zzyl.nursing.vo.health.HealthDataVo;
import com.zzyl.nursing.vo.health.SelectHealthDataVo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 老人家属Controller
 *
 * @author ruoyi
 * @date 2025-04-26
 */
@RestController
@RequestMapping("/member/user")
@Api(tags = "老人家属的接口")
public class FamilyMemberController extends BaseController {
    @Autowired
    private IFamilyMemberService familyMemberService;

    @PostMapping("/login")
    public AjaxResult login(@RequestBody UserLoginRequestDto dto) {
        LoginVo vo = familyMemberService.login(dto);
        return success(vo);
    }

    /**
     * 小程序端绑定老人
     *
     * @param dto
     * @return
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody FamilyMemberElderDto dto) {
        familyMemberService.add(dto);
        return success();
    }

    /**
     * 查询家人列表
     *
     * @return
     */
    @GetMapping("/my")
    public AjaxResult getMyFamilyMembers() {
        Long familyMemberId = UserThreadLocal.getUserId();
        List<FamilyMemberElder> list = familyMemberService.getMyFamilyMembers(familyMemberId);
        return success(list);
    }

    /**
     * 查询健康数据
     *
     * @param iotId
     * @return
     */
    @GetMapping("/queryServiceProperties/{iotId}")
    public AjaxResult queryServiceProperties(@PathVariable("iotId") String iotId) {
        List<HealthDataVo> healthData = familyMemberService.queryServiceProperties(iotId);
        return success(healthData);
    }

    /**
     * 按天统计查询指标数据
     */
    @GetMapping("/queryDeviceDataListByDay")
    public AjaxResult queryDeviceDataListByDay(
            @RequestParam String iotId,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Long startTime, // 或直接用 Long 接收时间戳
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Long endTime,
            @RequestParam(required = false) String functionId) { // 功能ID可选

        // 1. 调用 Service 层查询数据
        List<Map<String, Object>> dataList = familyMemberService.queryDeviceDataListByDay(iotId, startTime, endTime, functionId);
        return success(dataList);
    }

    /**
     * 周统计查询指标数据
     */
    @GetMapping("/queryDeviceDataListByWeek")
    public AjaxResult queryDeviceDataListByWeek(
            @RequestParam String iotId,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Long startTime, // 或直接用 Long 接收时间戳
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Long endTime,
            @RequestParam(required = false) String functionId) {
        List<Map<String, Object>> dataList = familyMemberService.queryDeviceDataListByWeek(iotId, startTime, endTime, functionId);
        return success(dataList);

    }

    /**
     * 查看家人列表（分页查询）
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list-by-page")
    public AjaxResult listByPage(Integer pageNum, Integer pageSize) {
        List<FamilyMemberELderVo> list = familyMemberService.listByPage(pageNum, pageSize);
        return success(list);
    }

    /**
     * 解绑家人
     */
    @DeleteMapping("/deleteById")
    public AjaxResult deleteById(@RequestParam Long id) {
        familyMemberService.deleteFamilyMemberById(id);
        return success();
    }

}






